package com.danciyixue.boot.dcyx.controller;

import com.danciyixue.boot.dcyx.service.SemesterService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.danciyixue.boot.dcyx.model.form.SemesterForm;
import com.danciyixue.boot.dcyx.model.query.SemesterQuery;
import com.danciyixue.boot.dcyx.model.vo.SemesterVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 学期信息前端控制层
 *
 * @author youlaitech
 * @since 2025-06-15 16:25
 */
@Tag(name = "学期信息接口")
@RestController
@RequestMapping("/api/v1/semester")
@RequiredArgsConstructor
public class SemesterController  {

    private final SemesterService semesterService;

    @Operation(summary = "学期信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('dcyx:semester:query')")
    public PageResult<SemesterVO> getSemesterPage(SemesterQuery queryParams ) {
        IPage<SemesterVO> result = semesterService.getSemesterPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增学期信息")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('dcyx:semester:add')")
    public Result<Void> saveSemester(@RequestBody @Valid SemesterForm formData ) {
        boolean result = semesterService.saveSemester(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取学期信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('dcyx:semester:edit')")
    public Result<SemesterForm> getSemesterForm(
        @Parameter(description = "学期信息ID") @PathVariable Long id
    ) {
        SemesterForm formData = semesterService.getSemesterFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改学期信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('dcyx:semester:edit')")
    public Result<Void> updateSemester(
            @Parameter(description = "学期信息ID") @PathVariable Long id,
            @RequestBody @Validated SemesterForm formData
    ) {
        boolean result = semesterService.updateSemester(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除学期信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('dcyx:semester:delete')")
    public Result<Void> deleteSemesters(
        @Parameter(description = "学期信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = semesterService.deleteSemesters(ids);
        return Result.judge(result);
    }
}
